package com.ehualu.esresult;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ehualu.esresult.utils.ESurlUtil;
import com.ehualu.esresult.utils.HttpUtil;
import com.ehualu.esresult.utils.LoadProperties;
import com.ehualu.esresult.utils.MysqlUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: 吴敬超
 * @Date: 2020/7/20 16:13
 */
public class OperationQuality {
    private static final Logger LOG = LoggerFactory.getLogger(OperationQuality.class);


    /*响应时间*/
    public static Map responsetimeRun(String indexname) {

        Map<String, Long> map = new HashMap<String, Long>(5);

        LoadProperties loadProc = LoadProperties.getInstance();
        String posturl = ESurlUtil.getUrl("index.vehicleorder");

        if (posturl.equals("")) {
            LOG.info("posturl 是空，不继续执行");
            map.put("countid30", 0L);
            map.put("avgtime", 0L);
            return map;
        }
        String postresponsetimejosn = loadProc.getValues(indexname, "");

        String postresponsetimejosnresult = HttpUtil.HttpPostWithJson(posturl, postresponsetimejosn);

        LOG.info("ES响应结果 postresponsetimejosnresult is :" + postresponsetimejosnresult);

        if (postresponsetimejosnresult.equals("DefaultReturnValue")) {

            LOG.info("ES请求结果是空，不继续执行");
            map.put("countid30", 0L);
            map.put("avgtime", 0L);
            return map;
        }


        /*30天订单数*/
        Integer countid30 = JSON.parseObject(postresponsetimejosnresult)
                .getJSONObject("aggregations")
                .getJSONObject("stats_monthly_sales")
                .getInteger("count");

        LOG.info("30天订单数 countid30 is :" + countid30);


        /*没有订单返回0*/
        if (countid30 == 0) {

            map.put("countid30", 0L);
            map.put("avgtime", 0L);
            return map;
        }

        /*30天订单分桶*/
        JSONArray responsetimebuckets30 = JSON.parseObject(postresponsetimejosnresult)
                .getJSONObject("aggregations")
                .getJSONObject("top_tags")
                .getJSONArray("buckets");


        Long sumtime = 0L;


        /*遍历30天订单分桶*/
        for (int i = 0; i < responsetimebuckets30.size(); i++) {

            JSONObject everybuckets = responsetimebuckets30.getJSONObject(i);

            /*每个桶内预约时间*/
            Long maxorderTime = everybuckets
                    .getJSONObject("maxorderTime")
                    .getLongValue("value_as_string");




            /*每个桶内接单时间*/
            Long maxreceiptTime = everybuckets
                    .getJSONObject("maxreceiptTime")

                    .getLongValue("value_as_string");
            sumtime = sumtime + (maxreceiptTime - maxorderTime);

        }

        LOG.info("sumtime is :" + sumtime);

        Long avgtime = sumtime / countid30;
        map.put("countid30", countid30.longValue());
        map.put("avgtime", avgtime);

        return map;

    }


    /*订单完成数*/
    public static Integer OrderCompletionRaterun(String indexname) {


        LoadProperties loadProc = LoadProperties.getInstance();
        String posturl = ESurlUtil.getUrl("index.vehicleorder");

        if (posturl.equals("")) {
            LOG.info("posturl 是空，不继续执行");


            return 0;
        }
        String postOrderCompletionNumjosn = loadProc.getValues(indexname, "");

        String postOrderCompletionNumjosnresult = HttpUtil.HttpPostWithJson(posturl, postOrderCompletionNumjosn);

        if (postOrderCompletionNumjosnresult.equals("DefaultReturnValue")) {

            LOG.info("ES请求结果是空，不继续执行");
            return 0;

        }

        LOG.info("ES响应结果 postOrderCompletionNumjosnresult is :" + postOrderCompletionNumjosnresult);




        /*30天订单完成数*/
        Integer countid30 = JSON.parseObject(postOrderCompletionNumjosnresult)
                .getJSONObject("aggregations")
                .getJSONObject("stats_monthly_sales")
                .getInteger("count");

        LOG.info("30天订单数 countid30 is :" + countid30);

        return countid30;


    }


    /*乘车人数*/
    public static Integer Numberofpassengersrun(String indexname) {


        LoadProperties loadProc = LoadProperties.getInstance();
        String posturl = ESurlUtil.getUrl("index.vehicleorder");

        if (posturl.equals("")) {
            LOG.info("posturl 是空，不继续执行");


            return 0;
        }

        String postNumberofpassengersjosn = loadProc.getValues(indexname, "");

        String postNumberofpassengersjosnresult = HttpUtil.HttpPostWithJson(posturl, postNumberofpassengersjosn);

        if (postNumberofpassengersjosnresult.equals("DefaultReturnValue")) {

            LOG.info("ES请求结果是空，不继续执行");
            return 0;

        }

        LOG.info("ES响应结果 postNumberofpassengersjosnresult is :" + postNumberofpassengersjosnresult);



        /*30天乘车总人数*/
        Integer sum30 = JSON.parseObject(postNumberofpassengersjosnresult)
                .getJSONObject("aggregations")
                .getJSONObject("sumpassengerNumber")
                .getInteger("value");

        LOG.info("30天乘车总人数 sum30 is :" + sum30);

        return sum30;


    }


    public static void main(String[] args) {

        while (true) {

            LOG.info("*******************循环执行函数*************************");

            LOG.info("*******************近30天执行计算结果*************************");

            Map<String, Long> map30responsetime = responsetimeRun("search.30responsetime");


            Long countid30 = map30responsetime.get("countid30");


            Long avgtime30 = map30responsetime.get("avgtime");


            LOG.info("30天订单数：" + countid30);
            LOG.info("30天平均接单时间（毫秒）：" + avgtime30);

            Double avgtime30s = (double) avgtime30 / 1000;
            LOG.info("30天平均接单时间（秒）：" + avgtime30s);

            Integer CompletionRaterun30 = OrderCompletionRaterun("search.30OrderCompletionRate");

            LOG.info("30天订单完成数：" + CompletionRaterun30);
            Double CompletionDouble30 = 0.0;
            if (CompletionRaterun30 != 0) {
                CompletionDouble30 = (double) CompletionRaterun30 / (double) countid30;

            }

            LOG.info("30天订单完成率：" + CompletionDouble30);

            Integer Numberofpassenger = Numberofpassengersrun("search.30Numberofpassengers");

            LOG.info("30天乘车人数：" + Numberofpassenger);

            Double douberofpassenger30 = 0.0;


            if (Numberofpassenger != 0) {
                douberofpassenger30 = (double) Numberofpassenger / 30;

            }

            LOG.info("30天平均乘车人数：" + douberofpassenger30);


            MysqlUtil mysqlUtil = new MysqlUtil();
            mysqlUtil.updateQuality("30", avgtime30.toString(), CompletionDouble30.toString(), douberofpassenger30.toString());

            LOG.info("*******************近60-30天执行计算结果*************************");
            Map<String, Long> map6030responsetime = responsetimeRun("search.6030responsetime");

            /*60-30天订单数*/
            Long countid6030 = map6030responsetime.get("countid30");

            /*60-30天平均响应时间*/
            Long avgtime6030 = map6030responsetime.get("avgtime");


            LOG.info("60-30天订单数：" + countid6030);
            LOG.info("60-30天平均接单时间（毫秒）：" + avgtime6030);


            Double avgtime6030s = (double) avgtime6030 / 1000;
            LOG.info("60-30天平均接单时间（秒）：" + avgtime6030s);



            /*60-30天订单完成数*/
            Integer CompletionRaterun6030 = OrderCompletionRaterun("search.6030OrderCompletionRate");

            LOG.info("60-30天订单完成数：" + CompletionRaterun6030);


            Double CompletionDouble6030 = 0.0;
            if (CompletionRaterun6030 != 0) {
                CompletionDouble6030 = (double) CompletionRaterun6030 / (double) countid6030;

            }

            LOG.info("60-30天订单完成率：" + CompletionDouble6030);

            Integer Numberofpassenger6030 = Numberofpassengersrun("search.6030Numberofpassengers");

            LOG.info("60-30天乘车人数：" + Numberofpassenger6030);


            Double douberofpassenger6030 = 0.0;


            if (Numberofpassenger6030 != 0) {
                douberofpassenger6030 = (double) Numberofpassenger6030 / 30;

            }

            LOG.info("60-30天平均乘车人数：" + douberofpassenger6030);
            mysqlUtil.updateQuality("60", avgtime6030.toString(), CompletionDouble6030.toString(), douberofpassenger6030.toString());


            try {
                /*24小时执行一次*/
                /*24 * 60 * 60 * 1000;*/
                Thread.sleep(24 * 60 * 60 * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }


    }
}
